package per.poker.chudadi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class PokerGen {

    private static final List<String> POKERS = new LinkedList<>();

    static void genNewCard() {

        POKERS.clear();
        String[] colors = {"♥", "♠", "♣", "♦"};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String number : numbers) {
            for (String color : colors) {
                POKERS.add(color + number);
            }
        }
        Collections.shuffle(POKERS);
    }

    static Map<String, List<String>> distributeCard() {

        Map<String, List<String>> map = new HashMap<>(8);
        List<List<String>> ls = CollUtil.split(POKERS, 13);

        ls.get(0).sort(PokerGen::compareCard);
        ls.get(1).sort(PokerGen::compareCard);
        ls.get(2).sort(PokerGen::compareCard);
        ls.get(3).sort(PokerGen::compareCard);
        map.put("east", ls.get(0));
        map.put("south", ls.get(1));
        map.put("west", ls.get(2));
        map.put("north", ls.get(3));

        print(map);
//        map.put("east", CollUtil.newArrayList("♥5","♠5","♣7","♥8","♠8","♠9","♦10","♠10","♥J","♣A","♥A","♠2","♠3"));
//        map.put("south", CollUtil.newArrayList("♣4","♠4","♣6","♥6","♥9","♣10","♦J","♣J","♠J","♦K","♦A","♦2","♦3"));
//        map.put("west", CollUtil.newArrayList("♦4","♥4","♦6","♠6","♦8","♣9","♣Q","♥Q","♣K","♥K","♠K","♥2","♣3"));
//        map.put("north", CollUtil.newArrayList("♦5","♣5","♦7","♥7","♠7","♣8","♦9","♥10","♦Q","♠Q","♠A","♣2","♥3"));

        return map;
    }

    private static int compareCard(String card1, String card2) {
        CardEnum cardEnum1 = EnumUtil.getBy(CardEnum.class, e -> ObjectUtil.equal(card1.substring(1), e.getCode()));
        CardEnum cardEnum2 = EnumUtil.getBy(CardEnum.class, e -> ObjectUtil.equal(card2.substring(1), e.getCode()));

        TypeEnum color1 = EnumUtil.getBy(TypeEnum.class, e -> ObjectUtil.equal(card1.substring(0, 1), e.getCode()));
        TypeEnum color2 = EnumUtil.getBy(TypeEnum.class, e -> ObjectUtil.equal(card2.substring(0, 1), e.getCode()));


        if (color1 == color2 && cardEnum1 != cardEnum2) {
            return Integer.compare(cardEnum1.getNum(), cardEnum2.getNum());
        } else if (color1 != color2 && cardEnum1 == cardEnum2) {
            return Integer.compare(color1.getNum(), color2.getNum());
        } else {
            return Integer.compare(cardEnum1.getNum(), cardEnum2.getNum());
        }

    }

    private static void print(Map<String, List<String>> map) {
        StringBuilder s1 = new StringBuilder();
        map.get("east").forEach(e -> s1.append("\"").append(e).append("\"").append(","));
        s1.delete(s1.length() - 1, s1.length());

        StringBuilder s2 = new StringBuilder();
        map.get("south").forEach(e -> s2.append("\"").append(e).append("\"").append(","));
        s2.delete(s2.length() - 1, s2.length());

        StringBuilder s3 = new StringBuilder();
        map.get("west").forEach(e -> s3.append("\"").append(e).append("\"").append(","));
        s3.delete(s3.length() - 1, s3.length());

        StringBuilder s4 = new StringBuilder();
        map.get("north").forEach(e -> s4.append("\"").append(e).append("\"").append(","));
        s4.delete(s4.length() - 1, s4.length());

        log.info("east:{}", s1);
        log.info("south:{}", s2);
        log.info("west:{}", s3);
        log.info("north:{}", s4);
    }

}
